package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: YHL
 * @time: 2021/2/24 19:47
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    /**
     * spu的分页查询
     * @param page
     * @param rows
     * @param id
     * @param cid
     * @param bid
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageQuery(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "id",required = false)Long id,
            @RequestParam(value = "categoryId",required = false)Long cid,
            @RequestParam(value = "brandId",required = false)Long bid,
            @RequestParam(value = "saleable",required = false)Boolean saleable
    ){

        return ResponseEntity.ok(this.goodsService.pageQuery(page,rows,id,cid,bid,saleable));
}

    /**
     * 商品上下架修改
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void>modifySaleable(
            @RequestParam("id")Long id,
            @RequestParam("saleable")Boolean saleable){
        this.goodsService.modifySaleable(id,saleable);
        return ResponseEntity.ok().build();
    }
    /**
     * 商品新增
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(
            @RequestBody SpuDTO spuDTO){
        this.goodsService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据商品id查询对应的商品信息
     * @param id
     * @return
     */
@GetMapping("/{id}")
public ResponseEntity<SpuDTO>queryGoodsById(@PathVariable("id")Long id){
    return ResponseEntity.ok(this.goodsService.queryGoodsById(id));

}

    /**
     * 商品修改业务
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(
            @RequestBody SpuDTO spuDTO){

        this.goodsService.updateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据spuId查询对应的spuDetail
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO>querySpuDetailById(@RequestParam("id")Long id){
        return ResponseEntity.ok(this.goodsService.querySpuDetailById(id));
    }

    /**
     * 根据spuId查询对应的sku的集合
     * @param spuId
     * @return
     */

    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>>listSkuBySpuID(@RequestParam("id")Long spuId){
        return ResponseEntity.ok(this.goodsService.listSkuBySpuID(spuId));
    }

    /**
     * 根据spuId查询对应的spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO>querySpuById(@PathVariable("id")Long id){

        return ResponseEntity.ok(this.goodsService.querySpuById(id));

    }
    /**
     * 规格参数集合以及每个参数对应的值的业务查询
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("spec/value")
    public ResponseEntity<List<SpecParamDTO>>listSpecParam(@RequestParam("id")Long id,
                                                           @RequestParam("searching")Boolean searching){

        return ResponseEntity.ok(this.goodsService.listSpecParam(id,searching));

    }



}
